/*
*/

#include <form.h>
#include <keypad.h>

#define	FACT(x,y)	((((x)<<SLOT_SHIFT)+(y>>2))/(y))
#define	PERIOD_36	2000
#define	PERIOD_38	1890

typedef struct _PACK{
    U32	fact;
    U32	tick;
    U32	time;
}PACK;

static KEY key211[KEY_PRESTORE]={
    {FORM_843,0,0x0809,0x40040100},		//1
    {FORM_843,0,0x8889,0x40040100},		//2
    {FORM_843,0,0x4849,0x40040100},		//3
    {FORM_843,0,0xc8c9,0x40040100},		//4
    {FORM_843,0,0x2829,0x40040100},		//5
    {FORM_843,0,0xa8a9,0x40040100},		//6
    {FORM_843,0,0x6869,0x40040100},		//7

    {FORM_843,0,0xe8e9,0x40040100},		//8
    {FORM_843,0,0x1819,0x40040100},		//9
    {FORM_843,0,0x9899,0x40040100},		//0
    {FORM_843,0,0xdcdd,0x40040100},		//11: Digi
    {FORM_843,0,0x9c9d,0x40040100},		//12: Disp
    {FORM_843,0,0xa0a1,0x40040100},		//13: Source
    {FORM_843,0,0x4c4d,0x40040100},		//14: Mute
    {FORM_843,0,0xbcbd,0x40040100},		//15: Power

    {FORM_843,0,0xacad,0x40040100},		//16: ChM
    {FORM_843,0,0x2c2d,0x40040100},		//17: ChP
    {FORM_843,0,0x8485,0x40040100},		//18: VolM
    {FORM_843,0,0x0405,0x40040100},		//19: VolP
    {FORM_843,0,0x4a4b,0x40040100},		//20: Menu
    {0},					//21: Enter
    {0},					//22: Exit
    {FORM_843,0,0xcbca,0x40040100},		//23: Info

    {FORM_843,0,0x9293,0x40040100},		//24: Ok
    {FORM_843,0,0xf2f3,0x40040100},		//25: Right
    {FORM_843,0,0xd2d3,0x40040100},		//26: Down
    {FORM_843,0,0x7273,0x40040100},		//27: Left
    {FORM_843,0,0x5253,0x40040100},		//28: Up
    {0},					//29
    {FORM_843,0,0x505d,0x40040d00},		//30: Play
    {FORM_843,0,0x606d,0x40040d00},		//31: Pluse

    {FORM_843,0,0x000d,0x40040d00},		//32: Stop
    {FORM_843,0,0x0000,0x40040100},		//33: Eject
    {FORM_843,0,0x0000,0x40040100},		//34: Rec
    {FORM_843,0,0x525f,0x40040d00},		//35: Ffwd
    {FORM_843,0,0x929f,0x40040d00},		//36: Bkwd
    {FORM_843,0,0x0000,0x40040100},		//37: Prev
    {FORM_843,0,0x0000,0x40040100},		//38: Next
    {FORM_843,0,0x0000,0x40040100},		//39: Subtitle

    {FORM_843,0,0x0000,0x40040100},		//40: Angle
    {FORM_843,0,0x0000,0x40040100},		//41: Zoom
    {FORM_843,0,0x0000,0x40040100}		//42: Lang
};

static KEY key_tcl[KEY_PRESTORE]={
    {FORM_083,0,0,0x0202807f},			//1
    {FORM_083,0,0,0x020240bf},			//2
    {FORM_083,0,0,0x0202c03f},			//3
    {FORM_083,0,0,0x020220df},			//4
    {FORM_083,0,0,0x0202a05f},			//5
    {FORM_083,0,0,0x0202609f},			//6
    {FORM_083,0,0,0x0202e01f},			//7

    {FORM_083,0,0,0x020210ef},			//8
    {FORM_083,0,0,0x0202906f},			//9
    {FORM_083,0,0,0x0202a25d},			//0
    {0},					//11: Digi
    {0},					//12: Disp
    {FORM_083,0,0,0x0202827d},			//13: Source
    {FORM_083,0,0,0x0202f00f},			//14: Mute
    {FORM_083,0,0,0x020250af},			//15: Power

    {FORM_083,0,0,0x02027887},			//16: ChM
    {FORM_083,0,0,0x0202f807},			//17: ChP
    {FORM_083,0,0,0x020238c7},			//18: VolM
    {FORM_083,0,0,0x0202a857},			//19: VolP
    {FORM_083,0,0,0x020202fd},			//20: Menu
    {0},					//21: Enter
    {0},					//22: Exit
    {0},					//23: Info

    {FORM_083,0,0,0x0202b04f},			//24: Ok
    {FORM_083,0,0,0x02028877},			//25: Right
    {FORM_083,0,0,0x0202708f},			//26: Down
    {FORM_083,0,0,0x020208f7},			//27: Left
    {FORM_083,0,0,0x0202d02f},			//28: Up
    {0},					//29
    {0},					//30: Play
    {0},					//31: Pluse

    {0},					//32: Stop
    {0},					//33: Eject
    {0},					//34: Rec
    {0},					//35: Ffwd
    {0},					//36: Bkwd
    {0},					//37: Prev
    {0},					//38: Next
    {0},					//39: Subtitle

    {0},					//40: Angle
    {0},					//41: Zoom
    {0}						//42: Lang
};

static U32	Decode00	(BEAM*,KEY*,U32);
static U32	Decode083	(BEAM*,KEY*,U32);
static U32	Decode103	(BEAM*,KEY*,U32);
static U32	Decode113	(BEAM*,KEY*,U32);
static U32	Decode133	(BEAM*,KEY*,U32);
static U32	Decode152	(BEAM*,KEY*,U32);
static U32	Decode177	(BEAM*,KEY*,U32);
static U32	Decode1		(BEAM*,KEY*,U32,U32,U32,U32);
static U32	Decode2		(BEAM*,KEY*,U32,U32,U32);
static U32	Decode3		(BEAM*,KEY*,U32,U32,U32);
static U32	Decode412	(BEAM*,KEY*,U32);
static U32	Decode443	(BEAM*,KEY*,U32);
static U32	Decode7		(BEAM*,KEY*,U32,U32,U32);
static U32	Decode810	(BEAM*,KEY*,U32);
static U32	Decode843	(BEAM*,KEY*,U32);
static U32	Decode883	(BEAM*,KEY*,U32);
static U32	DecodeDy	(BEAM*,KEY*,U32);
static U32	Encode00	(BEAM*,KEY*);
static U32	Encode083	(BEAM*,KEY*);
static U32	Encode088	(BEAM*,KEY*);
static U32	Encode1		(BEAM*,KEY*,U32,U32,U32,U32);
static U32	Encode103	(BEAM*,KEY*);
static U32	Encode112	(BEAM*,KEY*);
static U32	Encode113	(BEAM*,KEY*);
static U32	Encode133	(BEAM*,KEY*);
static U32	Encode152	(BEAM*,KEY*);
static U32	Encode177	(BEAM*,KEY*);
static U32	Encode2		(BEAM*,KEY*,U32,U32,U32);
static U32	Encode3		(BEAM*,KEY*,U32,U32,U32);
static U32	Encode412	(BEAM*,KEY*);
static U32	Encode443	(BEAM*,KEY*);
static U32	Encode7		(BEAM*,KEY*,U32,U32,U32);
static U32	Encode810	(BEAM*,KEY*);
static U32	Encode843	(BEAM*,KEY*);
static U32	Encode883	(BEAM*,KEY*);
static U32	Encode884	(BEAM*,KEY*);
static U32	EncodeDy	(BEAM*,KEY*);
static U32*	Pack1		(U32*,PACK*,KEY*,BEAM*,U32);
static U32*	Pack2		(U32*,PACK*,KEY*,BEAM*);
static U32	Unpack1		(U16*,KEY*,U32,U32,U32,U32);
static U32	Unpack2		(U16*,KEY*,U32,U32);

static U32(*decode[])(BEAM*,KEY*,U32)={
    Decode083,	//1
    Decode443,	//2
    Decode843,	//3
    Decode883,	//4
    Decode113,	//5
    Decode00,	//6
    Decode103,	//7
    Decode133,	//8
    Decode412,	//9
    DecodeDy,	//10 - 088
    DecodeDy,	//11 - 112
    Decode152,	//12
    Decode177,	//13
    DecodeDy,	//14 - 884
    Decode810,	//15
    DecodeDy,
    DecodeDy
};

static U32(*encode[])(BEAM*,KEY*)={
    Encode083,	//1
    Encode443,	//2
    Encode843,	//3
    Encode883,	//4
    Encode113,	//5
    Encode00,	//6
    Encode103,	//7
    Encode133,	//8
    Encode412,	//9
    Encode088,	//10
    Encode112,	//11
    Encode152,	//12
    Encode177,	//13
    Encode884,	//14
    Encode810,	//15
    EncodeDy,
    EncodeDy,
    EncodeDy
};

static U32 DecodeDy(BEAM *b,KEY *k,U32 i){
    return 0;
}

static U32 Decode00(BEAM *beam,KEY *key,U32 i){
    return 0;
}

static U32 Decode083(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    if(Decode1(beam,key,16,8,24,2)){
	if(beam->max1==8)
	    return 10;	//Encode088
	return i;
    }
    return 0;
}

static U32 Decode103(BEAM *beam,KEY *key,U32 i){
    return Decode3(beam,key,1,10,11)?i:0;
}

static U32 Decode113(BEAM *beam,KEY *key,U32 i){
    beam->exit=8;
    if(Decode3(beam,key,1,1,2)){
	if(beam->max0==2&&beam->max1==2){
	    Decode2(beam,key,1,1,2);
	    return 11;
	}
	return i;
    }
    return 0;
}

static U32 Decode133(BEAM *beam,KEY *key,U32 i){
    beam->exit=7;
    return Decode3(beam,key,1,3,4)?i:0;
}

static U32 Decode152(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    return Decode2(beam,key,1,5,6)?i:0;
}

static U32 Decode177(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    return Decode7(beam,key,1,7,8)?i:0;
}

static U32 Decode1(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 head,U32 val0){
    U32	    b,*p,v;
    PACK    c;

    if((beam->head1==head1)&&(beam->head0==head0)){
	b=beam->base;
	p=beam->form;
	v=*p++&CNT_MASK;
	if(v<(b>>(SLOT_SHIFT+1)))
	    v+=*p++&CNT_MASK;
	c.tick=head;
	c.time=v+(*p++&CNT_MASK);
	p=Pack1(p,&c,key,beam,val0);
	beam->head=beam->bits+3;
	beam->period=(U16)
	    ((c.time<<SLOT_SHIFT)/(c.tick*beam->repetition));
	return 1;
    }
    return 0;
}

static U32 Decode2(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 head){
    U32	    b,*p,v;
    PACK    c;

    if((beam->head1==head1)&&(beam->head0==head0)){
	b=beam->base;
	p=beam->form;
	v=*p++&CNT_MASK;
	if(v<(b>>(SLOT_SHIFT+1)))
	    v+=*p++&CNT_MASK;
	c.tick=head;
	c.time=v+(*p++&CNT_MASK);
	p=Pack2(p,&c,key,beam);
	beam->head=beam->bits+3;
	beam->period=(U16)
	    ((c.time<<SLOT_SHIFT)/(c.tick*beam->repetition));
	return 1;
    }
    return 0;
}

static U32 Decode3(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 head){
    U32	    b,*p,v;
    PACK    c;

    if((beam->head1==head1)&&(beam->head0==head0)){
	b=beam->base;
	p=beam->form;
	v=*p++&CNT_MASK;
	if(v<(b>>(SLOT_SHIFT+1)))
	    v+=*p++&CNT_MASK;
	c.tick=head;
	c.time=v+(*p++&CNT_MASK);
	p=Pack1(p,&c,key,beam,2);
	beam->head=beam->bits+3;
	beam->period=(U16)
	    ((c.time<<SLOT_SHIFT)/(c.tick*beam->repetition));
	return 1;
    }
    return 0;
}

static U32 Decode412(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    return Decode2(beam,key,4,1,5)?i:0;
}

static U32 Decode443(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    return Decode3(beam,key,4,4,8)?i:0;
}

static U32 Decode7(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 head){
    U32	    b,*p,v;
    PACK    c;

    if((beam->head1==head1)&&(beam->head0==head0)){
	b=beam->base;
	p=beam->form;
	v=*p++&CNT_MASK;
	if(v<(b>>(SLOT_SHIFT+1)))
	    v+=*p++&CNT_MASK;
	c.tick=head;
	c.time=v+(*p++&CNT_MASK);
	p=Pack1(p,&c,key,beam,4);
	beam->head=beam->bits+3;
	beam->period=(U16)
	    ((c.time<<SLOT_SHIFT)/(c.tick*beam->repetition));
	return 1;
    }
    return 0;
}

static U32 Decode810(BEAM *beam,KEY *key,U32 i){
    beam->exit=100;
    return Decode3(beam,key,8,1,9)?i:0;
}

static U32 Decode843(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    return Decode3(beam,key,8,4,12)?i:0;
}

static U32 Decode883(BEAM *beam,KEY *key,U32 i){
    beam->exit=max(beam->head0,beam->head1);
    if(Decode3(beam,key,8,8,16)){
	if(beam->max1==4)
	    return 14;
	return i;
    }
    return 0;
}

static U32 Encode00(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,0,0,3);
    return 1;
}

static U32 Encode083(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,16,8,3);
    return 1;
}

static U32 Encode088(BEAM *beam,KEY *key){
    U16	*d,p;
    U32	i;

    d=beam->wave;
    p=beam->pulse;
    *d++=0;
    *d++=0;
    for(i=16;i;i--)
	*d++=p;
    for(i=8;i;i--)
	*d++=0;
    beam->head=(U16)Unpack1(d,key,8,p,1,3)+26;
    return 1;
}

static U32 Encode103(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,1,10,10);
    return 1;
}

static U32 Encode112(BEAM *beam,KEY *key){
    beam->head=(U16)Encode2(beam,key,1,1,3);
    return 1;
}

static U32 Encode113(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,1,1,3);
    return 1;
}

static U32 Encode133(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,1,3,3);
    return 1;
}

static U32 Encode152(BEAM *beam,KEY *key){
    beam->head=(U16)Encode2(beam,key,1,5,2);
    return 1;
}

static U32 Encode177(BEAM *beam,KEY *key){
    beam->head=(U16)Encode1(beam,key,1,7,3,7);
    return 1;
}

static U32 Encode412(BEAM *beam,KEY *key){
    beam->head=(U16)Encode2(beam,key,4,1,2);
    return 1;
}

static U32 Encode443(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,4,4,3);
    return 1;
}

static U32 Encode810(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,8,4,3);
    return 1;
}

static U32 Encode843(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,8,4,3);
    return 1;
}

static U32 Encode883(BEAM *beam,KEY *key){
    beam->head=(U16)Encode3(beam,key,8,8,3);
    return 1;
}

static U32 Encode884(BEAM *beam,KEY *key){
    beam->head=(U16)Encode1(beam,key,8,8,2,4);
    return 1;
}

static U32 EncodeDy(BEAM *b,KEY *k){
    return 0;
}

static U32 Encode1(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 fac0,U32 fac1){
    U16	*d,p;
    U32	i;

    d=beam->wave;
    p=beam->pulse;
    *d++=0;
    *d++=0;
    for(i=head1;i;i--)
	*d++=p;
    for(i=head0;i;i--)
	*d++=0;
    return Unpack1(d,key,beam->head-2,p,fac0,fac1)+head0+head1+2;
}

static U32 Encode2(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 fact){
    U16	*d,p;
    U32	i;

    d=beam->wave;
    p=beam->pulse;
    *d++=0;
    *d++=0;
    for(i=head1;i;i--)
	*d++=p;
    for(i=head0;i;i--)
	*d++=0;
    return Unpack2(d,key,beam->head-2,p)+head0+head1+2;
}

static U32 Encode3(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 fact){
    U16	*d,p;
    U32	i;

    d=beam->wave;
    p=beam->pulse;
    *d++=0;
    *d++=0;
    for(i=head1;i;i--)
	*d++=p;
    for(i=head0;i;i--)
	*d++=0;
    return Unpack1(d,key,beam->head-2,p,1,3)+head0+head1+2;
}

static U32 Encode7(BEAM *beam,KEY *key,U32 head1,U32 head0,U32 fact){
    U16	*d,p;
    U32	i;

    d=beam->wave;
    p=beam->pulse;
    *d++=0;
    *d++=0;
    for(i=head1;i;i--)
	*d++=p;
    for(i=head0;i;i--)
	*d++=0;
    return Unpack1(d,key,beam->head-2,p,1,3)+head0+head1+2;
}

static U32* Pack1(U32 *src,PACK *pack,KEY *key,BEAM *beam,U32 val0){
    U32	c,e,i,k,v;
    U32	bs,bt,tm,tk,x0,x1,xt;
    U32	*d;

    tm=pack->time;
    tk=pack->tick;
    bs=beam->base;
    xt=beam->exit;
    bt=bs>>(SLOT_SHIFT+1);
    x0=x1=0;
    d=&key->data2;
    for(k=64;k;k-=32){
	v=0;
	for(i=32;i;i--){
	    do{
		if(!(e=*src++))
		    goto L1;
		e&=CNT_MASK;
		tm+=e;
	    }while(e<bt);
	    tk++;
	    c=FACT(e,bs);
	    if(c>x0)
		x0=c;
	    do{
		if(!(e=*src++))
		    goto L1;
		e&=CNT_MASK;
		tm+=e;
	    }while(e<bt);
	    c=FACT(e,bs);
	    if(c>xt){
		tm-=e;
L1:		*d=v<<i;
		goto L2;
	    }
	    if(c>x1)
		x1=c;
	    tk+=c;
	    v<<=1;
	    if(c>val0)
		v|=1;
	    continue;
	}
	*d--=v;
    }
L2: pack->tick=tk;
    pack->time=tm;
    beam->bits=(U8)(95-k-i);
    beam->max0=(U8)x0;
    beam->max1=(U8)x1;
    return src;
}

static U32* Pack2(U32 *src,PACK *pack,KEY *key,BEAM *beam){
    U32	c,e,i,k,v;
    U32	bs,bt,tm,tk,xt;
    U32	*d;

    tm=pack->time;
    tk=pack->tick;
    bs=beam->base;
    xt=beam->exit;
    bt=bs>>(SLOT_SHIFT+1);
    d=&key->data2;
    for(k=64;k;k-=32){
	v=0;
	for(i=32;i;i--){
	    do{
		if(!(e=*src++))
		    goto L1;
		e&=CNT_MASK;
		tm+=e;
	    }while(e<bt);
	    c=FACT(e,bs);
	    if(c>xt){
		tm-=e;
L1:		*d=v<<i;
		goto L2;
	    }
	    tk+=c;
	    v<<=1;
	    if(c>1)
		v|=1;
	    continue;
	}
	*d--=v;
    }
L2: pack->tick=tk;
    pack->time=tm;
    beam->bits=(U8)(95-k-i);
    return src;
}

static void Slot(SLOT *slot,U32 tim){
    U32	 t,tm,tn;
    SLOT *o,*q,*x;

    t=tim<<SLOT_SHIFT;
    tn=t/4;
    tm=t+tn;
    tn=t-tn;
    for(x=slot+(SLOT_LEN/2-1);slot<x;slot++){
	if(tn>slot->period){
	    for(q=x-1;q>slot;q--){
		o=q-1;
		if(o->count){
		    q->sum=o->sum;
		    q->count=o->count;
		    q->period=o->period;
		}
	    }
	    slot->sum=tim;
	    slot->count=1;
	    slot->period=tim<<SLOT_SHIFT;
	    return;
	}
	else if(tm>slot->period){
	    slot->sum=t=slot->sum+tim;
	    slot->count++;
	    slot->period=(t<<SLOT_SHIFT)/slot->count;
	    return;
	}
    }
}

static U32 Unpack1(U16 *dst,KEY *key,U32 length,U32 pulse,U32 fac0,U32 fac1){
    U32	*s;
    S32	a;
    U32	i,k,r,v;

    r=0;
    s=&key->data2;
    for(a=length;a>0;a-=32){
	v=*s--;
	for(i=a>32?32:a;i;i--){
	    *dst++=(U16)pulse;
	    if(v&0x80000000){
		for(k=fac1;k;k--)
		    *dst++=0;
		r+=fac1+1;
	    }
	    else{
		for(k=fac0;k;k--)
		    *dst++=0;
		r+=fac0+1;
	    }
	    v<<=1;
	}
    }
    *dst++=(U16)pulse;
    *dst++=0;
    *dst++=0;
    return r+3;
}

static U32 Unpack2(U16 *dst,KEY *key,U32 length,U32 pulse){
    U32	*s;
    S32	a;
    U32	i,r,v;
    U16	u;

    r=0;
    s=&key->data2;
    u=(U16)pulse;
    for(a=length;a>0;a-=32){
	v=*s--;
	for(i=a>32?32:a;i;i--){
	    *dst++=u;
	    r++;
	    if(v&0x80000000){
		*dst++=u;
		r++;
	    }
	    v<<=1;
	    u=u?0:(U16)pulse;
	}
    }
    *dst++=0;
    *dst++=0;
    *dst++=0;
    return r+3;
}

//-----------------------------------------------------------------------------

U32 Decode(BEAM *beam,KEY *key,SLOT *slot){
    U32	i,r;

    for(i=1;i<=countof(decode);i++){
	if(r=decode[i-1](beam,key,i)){
	    key->vendor=(U8)r;
	    return r;
	}
    }
    return 0;
}

U32 Encode(BEAM *beam,KEY *key){
    return key->vendor?encode[key->vendor-1](beam,key):0;
}

U32 Fact(BEAM *beam,SLOT *slot){
    U32	 c,f,n,*s,v;
    SLOT *t,*x;

    slot->count=slot->period=slot->sum=0;
    for(t=slot+1,x=slot+SLOT_LEN;t<x;t++)
	*t=*slot;
    f=9;
    t=slot+SLOT_BURR;
    for(s=beam->form;v=*s;s++){
	if(n=v>>CNT_SHIFT){
	    v&=CNT_MASK;
	    slot->sum=f=slot->sum+v;
	    slot->count+=n+1;
	    slot->period=f=(f<<SLOT_SHIFT)/slot->count;
	    f>>=SLOT_SHIFT;
	    Slot(slot+SLOT_HI,v+f);
	}
	else{
	    if(v<f*3/2){
		t->sum=f=t->sum+v;
		t->count++;
		t->period=(f<<SLOT_SHIFT)/t->count;
	    }
	    else Slot(slot+SLOT_LO,v-f);
	}
    }
    c=f<<(SLOT_SHIFT+2);
    n=0xffffffff;
    for(t=slot+SLOT_LO,x=slot+SLOT_LEN;t<x;t++)if((v=t->period)&&(n>v)&&(v>c))
	n=v;
    if(n!=0xffffffff){
	v=n<<SLOT_SHIFT;
	for(t=slot+SLOT_LO,x=slot+SLOT_LEN;t<x;t++)
	    t->fact=FACT(t->period,v);
	beam->repetition=(U8)((n+(slot->period>>1))/slot->period);
	s=beam->form;
	v=FACT((*s++&CNT_MASK)+f,n);
	if(!v)
	    v=FACT((*s++&CNT_MASK)+f,n);
	beam->head1=(U8)v;
	beam->head0=(U8)FACT((*s++&CNT_MASK)-f,n);
    }
    else n=0;
    beam->base=n;
    beam->period=(U16)slot->period;
    return n;
}

void Load(U32 device,KEY *buff,U32 len,U32 extra){
    KEY	*d,*s,*x;

    d=buff+1;
    if(device==DEV_PANA)
	s=key211;
    else s=key_tcl;
    for(x=s+KEY_PRESTORE;s<x;d++,s++)
	*d=*s;
}
